package com.fhao.spring.selfblog.service;

import com.fhao.spring.selfblog.mapper.BlogInfoMapper;
import com.fhao.spring.selfblog.model.BlogInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class BlogService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public List<BlogInfo> getList() {
        return blogInfoMapper.selectAll();
    }

    public BlogInfo getBlogDetail(Integer blogId) {
        return blogInfoMapper.selectById(blogId);
    }

    public BlogInfo selectByid(Integer blogId) {
        return blogInfoMapper.selectById(blogId);
    }

    public Boolean insertBook(BlogInfo blogInfo) {
        //如果发生异常,添加文章自然会失败
        try {
            Integer result = blogInfoMapper.insertBlog(blogInfo);
            if(result == 1) {
                return true;
            }
        }catch (Exception e) {
            log.error("添加图书失败,e : ",e);
        }
        return false;
    }

    public boolean update(BlogInfo blogInfo) {
        try{
            Integer result = blogInfoMapper.updateBlog(blogInfo);
            if(result == 1) {
                return true;
            }
        }catch (Exception e) {
            log.error("删除文章失败,e: ",e);
        }
        return false;
    }
}





















